//------------------------------------------------------------------------------
#include <QtGui>
#include <QClipboard>
#include <QSqlRecord>
#include "CMyTableWidgetItem.h"
#include "CFindResultFrm.h"
//------------------------------------------------------------------------------
CFindResultFrm::CFindResultFrm(QWidget *parent, QSqlQuery *query, QStringList *header, QSize size, QPoint point, int colSort, Qt::SortOrder order, int goToLigne) : QDialog(parent, Qt::WindowMaximizeButtonHint | Qt::WindowCloseButtonHint)
{
	int recAff, X, Y, numCols;
	QString recAffS;
	QSqlRecord rec=query->record();
	
	setupUi(this);
	_result="";
	findText="";
	
	fontTemplate="<font color='%1'>%2</font>";
	
	lblSaisie->setText(fontTemplate.arg("#0000FF", ""));
	
	recAff=query->size();
	
	table->setRowCount(recAff);
	table->setColumnCount(numCols=rec.count());	
	table->setSortingEnabled(false);
	table->setHorizontalHeaderLabels(*header);
	table->setContextMenuPolicy(Qt::CustomContextMenu);
	
	Y=0;
	while(query->next())
	{
		rec=query->record();
		for(X=0;X<numCols;X++)
		{
			CMyTableWidgetItem *item;
			
			item=new CMyTableWidgetItem(rec.value(X));
			
			table->setItem(Y, X, item);
		}
		Y++;
	}
	
	recAffS.setNum(recAff);
	setWindowTitle(tr("Résultat de la recherche (")+recAffS+(QString)+")");
	
	table->resizeColumnsToContents();
	table->setSortingEnabled(true);
	table->sortByColumn(colSort, order);
	
	_colSort=colSort;
	_sortOrder=order;
	
	if(size.isValid())
	{
		resize(size);
	}
	
	if(!point.isNull())
	{
		move(point);
	}
	
	if(goToLigne >= 0 && goToLigne < table->rowCount())
	{
		QModelIndex mi=table->indexAt(QPoint(0, 0));
		
		table->setCurrentCell(goToLigne, 0);
		table->scrollTo(mi.sibling(goToLigne, 0), QAbstractItemView::PositionAtTop);	
	}
	
	makeMenu();
}
//------------------------------------------------------------------------------
void CFindResultFrm::keyPressEvent(QKeyEvent * event)
{
	QString color=table->getAscending()==Qt::AscendingOrder ? "#0000FF" : "#FF0000";
	
	switch(event->key())
	{
		case Qt::Key_Backspace:
			if(!findText.isEmpty())
			{
				findText.truncate(findText.length()-1);
				lblSaisie->setText(fontTemplate.arg(color, findText));
				table->find(findText);
			}
			break;
		case Qt::Key_Delete:
			findText="";
			lblSaisie->setText(fontTemplate.arg(color, findText));
			table->find(findText);
			break;
		case Qt::Key_Enter:
		case Qt::Key_Return:
			_result=table->item(table->currentRow(), 0)->text();
			_goToLigne=table->currentRow();
			accept();
			break;
		default:
			if(event->text() >= " ")
			{
				findText+=event->text();
				lblSaisie->setText(fontTemplate.arg(color, findText));
				table->find(findText);
			}
			break;
	}
	QDialog::keyPressEvent(event);
}
//-----------------------------------------------------------------------------------
void CFindResultFrm::makeMenu(void)
{
	popupMenu=new QMenu(table);
	
	actActionExport=new QAction(tr("&Exporter le contenu de la table"), this);
	connect(actActionExport, SIGNAL(triggered()), this, SLOT(onActionExport_triggered()));
	
	popupMenu->addAction(actActionExport);
}
//------------------------------------------------------------------------------
void CFindResultFrm::on_table_ascendingChange(Qt::SortOrder ascending)
{
	lblSaisie->setText(fontTemplate.arg(ascending==Qt::AscendingOrder ? "#0000FF" : "#FF0000", findText));
	_sortOrder=ascending;
	table->find(findText);
}
//------------------------------------------------------------------------------
void CFindResultFrm::on_table_columnChange(int col)
{
	_colSort=col;
	findText="";
	lblSaisie->setText(fontTemplate.arg("#0000FF", findText));
	table->find(findText);
}
//------------------------------------------------------------------------------
void CFindResultFrm::on_table_valid(int row, int col)
{
	_result=table->item(row, 0)->text();
	_goToLigne=row;
	accept();
}
//------------------------------------------------------------------------------
void CFindResultFrm::on_table_cellDoubleClicked(int row, int col)
{
	_result=table->item(row, 0)->text();
	_goToLigne=row;
	accept();
}
//------------------------------------------------------------------------------
void CFindResultFrm::on_table_customContextMenuRequested(const QPoint & pos)
{
	QPoint p=pos;
	
	p.rx()+=table->verticalHeader()->width();
	p.ry()+=table->horizontalHeader()->height();
	
	popupMenu->popup(table->mapToGlobal(p));
}
//------------------------------------------------------------------------------
void CFindResultFrm::onActionExport_triggered(void)
{
	table->exportCSV(tr("resultat"));
}
//------------------------------------------------------------------------------

